package com.hq.fxh.No3_TuiGuang.MyMoBan.Activity;

import android.content.Context;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

 import com.hq.fxh.R;
import com.hq.fxh.Utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Lenovo on 2017/4/4.
 */
public class BigBusinessAdAdapter extends RecyclerView.Adapter<BigBusinessAdAdapter.MyViewHolder> {

    private Context context;
    private List<AdBannerInfo> datas;
    private List<AdBannerInfo> bussList = new ArrayList<>();
    private int clickPosition = -1;

    public BigBusinessAdAdapter(Context context, List<AdBannerInfo> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_big_ad_banner, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder myViewHolder, final int position) {
        final AdBannerInfo data = datas.get(position);
        myViewHolder.checkBox.setChecked(data.getAdvert_detail().getSelected() != null ? data.getAdvert_detail().getSelected() : false);
        data.getAdvert_detail().setSelected(data.getAdvert_detail().getSelected() != null ? data.getAdvert_detail().getSelected() : false);
        data.getAdvert_detail().setBigAdSelected(data.getAdvert_detail().getBigAdSelected() != null ? data.getAdvert_detail().getBigAdSelected() : false);
         GlideUtils.loadImage(context,data.getAdvert_detail().getPic(), myViewHolder.img);
//        myViewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (!buttonView.isPressed()) {
//                    return;
//                }
//                for (AdBannerInfo dataBean : datas) {
//                    dataBean.getAdvert_detail().setSelected(false);
//                }
//                if (isChecked) {
//                    data.getAdvert_detail().setSelected(true);
//                }
//                notifyDataSetChanged();
//                bussList.clear();
//                if (isChecked) {
//                    bussList.add(data);
//                }
//            }
//        });
        myViewHolder.bigAd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (AdBannerInfo dataBean : datas) {
                    dataBean.getAdvert_detail().setSelected(false);
                }
                if (clickPosition == position) {//点击同一位置
                    data.getAdvert_detail().setSelected(data.getAdvert_detail().getBigAdSelected());
                    data.getAdvert_detail().setBigAdSelected(!data.getAdvert_detail().getBigAdSelected());
                } else {//切换点击
                    data.getAdvert_detail().setSelected(true);
                    data.getAdvert_detail().setBigAdSelected(false);
                }
                bussList.clear();
                if (data.getAdvert_detail().getSelected()) {
                    bussList.add(data);
                }
                notifyDataSetChanged();
                clickPosition = position;
            }
        });
    }

    public List<AdBannerInfo> getBigBussList() {
        return bussList;
    }

    @Override
    public int getItemCount() {
        return datas != null ? datas.size() : 0;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        public View view;
        ImageView img;
        AppCompatCheckBox checkBox;
        RelativeLayout bigAd;

        public MyViewHolder(View itemView) {
            super(itemView);
            this.view = itemView;
            this.img = (ImageView) itemView.findViewById(R.id.banner_img);
            this.checkBox = (AppCompatCheckBox) itemView.findViewById(R.id.busAdCheck);
            this.bigAd = (RelativeLayout) itemView.findViewById(R.id.rl_bigAd);
        }
    }
}